﻿namespace com.stifel.BranchMgmt
{
    using System.Linq;

    using com.stifel.BranchMgmtDao;
    using com.stifel.Extensions.SharePointExtensions;
    using com.stifel.SharePoint;

    using Microsoft.SharePoint;

    public class StifelBranchTypes_Definition : StifelBaseDefinition
    {
        #region Fields

        const string LOOKUPSITECOLUMNS = "Stifel-BranchType.Lookup.xml";

        #endregion Fields

        #region Constructors

        public StifelBranchTypes_Definition( SPFeatureReceiverProperties properties )
            : base(properties)
        {
        }

        ~StifelBranchTypes_Definition( )
        {
            Dispose( false );
        }

        #endregion Constructors

        #region Methods

        public override void Create( )
        {
            base.Create( );
            CreateContentType( getContentTypeDefn( ) );
            CreateList( getListDefn( ) );
            CreateSiteColumns( GetFieldDefinitionsAsXml( LOOKUPSITECOLUMNS ) );
            loadList( );
        }

        public override void Delete( )
        {
            base.Delete( );
            DeleteList( getListDefn( ), false );
            DeleteContentType( getContentTypeDefn( ) );
            DeleteSiteColumns( GetFieldDefinitionsAsXml( LOOKUPSITECOLUMNS ) );
        }

        ContentTypeDefn getContentTypeDefn( )
        {
            return new ContentTypeDefn
            {
                Name = Item.GetContentTypeName<StifelBranchType>( ),
                ContentTypeId = Item.GetContentTypeId<StifelBranchType>( ),
                WebLevel = true,
                Group = "Stifel",
                Description = "Describes a type of branch.",
                Fields = new FieldDefn[ ] {
                }.Distinct( ).ToList( )
            };
        }

        ListDefn getListDefn( )
        {
            return new ListDefn
            {
                RootFolderName = StifelBranchTypeDao.ListName,
                Title = "Stifel Branch Types",
                Description = "Contains branch types.",
                ContentTypes = new string[ ]{
                    Item.GetContentTypeName<StifelBranchType>()
                }.ToList( )
            };
        }

        void loadList( )
        {
            StifelBranchTypeDao dao = new StifelBranchTypeDao( Properties.GetWebUrl( ) );
            dao.Clear( );
            dao.Add( "Full Service", "Convenience" );
        }

        #endregion Methods
    }
}